package edu.cuit.project.dao;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;

import edu.cuit.project.domain.Expert;
import static org.hibernate.criterion.Example.create;

/**
 * A data access object (DAO) providing persistence and search support for
 * Expert entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see edu.cuit.project.domain.Expert
 * @author MyEclipse Persistence Tools
 */

public class ExpertDAO extends BaseHibernateDAO<Expert> {
	private static final Log log = LogFactory.getLog(ExpertDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String GENDER = "gender";
	public static final String CELLPHONENUM = "cellphonenum";
	public static final String CAREER = "career";
	public static final String IDCARD = "idcard";
	public static final String POLITICAL_STATUS = "politicalStatus";
	public static final String MARRIAGE_STATUS = "marriageStatus";
	public static final String ADDRESS = "address";
	public static final String EDUCATION = "education";
	public static final String STATE = "state";

	public void save(Expert transientInstance) {
		super.sava(transientInstance);
	}

	public void delete(Expert persistentInstance) {
		log.debug("deleting Expert instance");
		try {
			getSession().clear();
			getSession().beginTransaction();
			getSession().delete(persistentInstance);
			getSession().beginTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Expert findById(java.lang.String id) {
		log.debug("getting Expert instance with id: " + id);
		try {
			Expert instance = (Expert) getSession().get(
					"edu.cuit.project.domain.Expert", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Expert> findByExample(Expert instance) {
		log.debug("finding Expert instance by example");
		try {
			List<Expert> results = (List<Expert>) getSession()
					.createCriteria("edu.cuit.project.domain.Expert")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Expert instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Expert as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Expert> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<Expert> findByGender(Object gender) {
		return findByProperty(GENDER, gender);
	}

	public List<Expert> findByCellphonenum(Object cellphonenum) {
		return findByProperty(CELLPHONENUM, cellphonenum);
	}

	public List<Expert> findByCareer(Object career) {
		return findByProperty(CAREER, career);
	}

	public List<Expert> findByIdcard(Object idcard) {
		return findByProperty(IDCARD, idcard);
	}

	public List<Expert> findByPoliticalStatus(Object politicalStatus) {
		return findByProperty(POLITICAL_STATUS, politicalStatus);
	}

	public List<Expert> findByMarriageStatus(Object marriageStatus) {
		return findByProperty(MARRIAGE_STATUS, marriageStatus);
	}

	public List<Expert> findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List<Expert> findByEducation(Object education) {
		return findByProperty(EDUCATION, education);
	}

	public List<Expert> findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findAll() {
		log.debug("finding all Expert instances");
		try {
			String queryString = "from Expert";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Expert merge(Expert detachedInstance) {
		log.debug("merging Expert instance");
		try {
		
			getSession().beginTransaction();
			
			Expert result = (Expert) getSession().merge(detachedInstance);
			getSession().beginTransaction().commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Expert instance) {
		log.debug("attaching dirty Expert instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Expert instance) {
		log.debug("attaching clean Expert instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}